﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MSADC.Workflow;
using System.Activities;
using System.Activities.Statements;
using System.Activities.XamlIntegration;
using System.Xaml;
using System.Text;
using System.IO;
using System.Xml;

namespace TenMinuteGuide
{
    public class TestWorkflowLookup : IWorkflowLookup
    {
        public string GetWorkflowDefinition(Guid workflowTemplateId)
        {
            // Always return the same workflow, regardless of the template Id.
            // Your implementation should vary!
            return BuildSimpleWorkflow();
        }

        private string BuildSimpleWorkflow()
        {
            ActivityBuilder builder = new ActivityBuilder();
            builder.Implementation = new Sequence();

            StringBuilder sb = new StringBuilder();

            // Important to exclude the XML declaration here, otherwise you'll get a WCF error
            using (XmlWriter writer = XmlWriter.Create(sb, new XmlWriterSettings { OmitXmlDeclaration = true}))
            {
                XamlWriter xw = ActivityXamlServices.CreateBuilderWriter(new XamlXmlWriter(writer, new XamlSchemaContext()));
                XamlServices.Save(xw, builder);
            }

            string xaml = sb.ToString();

            return xaml;
        }
    }
}